#!/bin/bash

set -e

echo -e "\n\033[32m\t*********compile llvm_19*********\033[0m\n"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

CURRENT_DIR=$(cd $(dirname $0);pwd)
SOURCE_PATH="${CURRENT_DIR}/llvm"

function prepare_env() {
  if [[ ! -d "$SOURCE_PATH" ]]; then
    mkdir -p "$SOURCE_PATH"
  fi
}

function pre_download_llvm15_code() {
  echo "---------------pre_download_llvm15_code start---------------"
  pushd $SOURCE_PATH > /dev/null
    rm -rf .repo/
    repo init -u https://gitee.com/OpenHarmony/manifest.git -b master -m llvm-19.1.7-toolchain.xml 
    repo sync -c 
    repo forall -c 'git lfs pull'
  popd > /dev/null
  echo "---------------pre_download_llvm15_code succeed---------------"
}


function pre_build_llvm15_code() {
  echo "---------------pre_build_llvm15_code start---------------"
  pushd $SOURCE_PATH > /dev/null
    ./toolchain/llvm-project/llvm-build/env_prepare.sh
    python3 ./toolchain/llvm-project/llvm-build/build.py --no-build windows,lldb-server
  popd > /dev/null
  echo "---------------pre_build_llvm15_code download---------------"
}

prepare_env
pre_download_llvm15_code
pre_build_llvm15_code